package com.ww.util.guava;

import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;

import java.util.Map;

public class CollectionFilter {

    public static void main(String[] args) {
        /**
         * 按照条件过滤
         */
        ImmutableList<String> names = ImmutableList.of("begin", "code", "Guava", "Java");
        Iterable<String> filtered = Iterables.filter(names, Predicates.or(Predicates.equalTo("Guava"), Predicates.equalTo("Java")));
        /**
         * 打印：[Guava, Java]
         */
        System.out.println(filtered);

        /**
         * 使用自定义回调方法对Map的每个Value进行操作
         */
        ImmutableMap<String, Integer> m = ImmutableMap.of("begin", 12, "code", 15);
        Map<String, Integer> m2 = Maps.transformValues(m, input -> {
            if (input > 12) {
                return input;
            } else {
                return input + 1;
            }
        });
        /**
         * 打印：{begin=13, code=15}
         */
        System.out.println(m2);
    }
}
